package io.renren.modules.sys.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.UserEntity;
import io.renren.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sys/userentity")
public class UserController extends AbstractController {
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        Query query=new Query(params);

        int total=userService.queryTotal();
        List<UserEntity> list=userService.queryList(query);
        PageUtils pageUtils=new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtils);
    }

    @RequestMapping("delete")
    public R delete(@RequestBody Integer[] id){
        userService.deleteBatch(id);
        return R.ok();
    }

    @RequestMapping("save")
    public R save(@RequestBody UserEntity u){

        ValidatorUtils.validateEntity(u);
        userService.save(u);
        return R.ok();
    }

    @RequestMapping("update")
    public R update(@RequestBody UserEntity u){
        System.out.println("进入update");
        ValidatorUtils.validateEntity(u);

        userService.update(u);
        return R.ok();
    }

    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        UserEntity userEntity=userService.queryObject(id);
        return R.ok().put("user", userEntity);
    }



}
